package com.sp.api.springbootapi.service;

import com.sp.api.springbootapi.dao.TGoodsInfoMapper;
import com.sp.api.springbootapi.model.TGoodsInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by weidewang on 2018/7/6
 */
@Service
public class TGoodsServiceImpl implements TGoodsService {
    @Resource
    private TGoodsInfoMapper mapper;

    @Override
    public void addGoods(TGoodsInfo info) {
        mapper.insert(info);
    }

    @Override
    public void deleteGoods(String code) {
        mapper.deleteByPrimaryKey(code);
    }

    @Override
    public int getGoodsRest(String code) {
        TGoodsInfo tGoodsInfo = mapper.selectByPrimaryKey(code);
        if (tGoodsInfo == null) {
            return 0;
        }
        return tGoodsInfo.getAmount();
    }

    /**
     * 基于版本
     * @param code
     * @param buys
     * @return
     */
    @Override
    public boolean updateGoodsAmountByVersion(String code, int buys) {
        TGoodsInfo tGoodsInfo = mapper.selectByPrimaryKey(code);
        if (tGoodsInfo == null) {
            return false;
        }
        Integer version = tGoodsInfo.getVersion();
        Integer amount = tGoodsInfo.getAmount();
        if (amount < buys) {
            return false;
        }
        return mapper.updateAmountByVersion(code, buys, version) > 0 ? true : false;
    }

    @Override
    public boolean updateGoodsAmountByState(String code, int buys) {
        return mapper.updateAmountByState(code, buys) > 0 ? true : false;
    }

    @Override
    public boolean updateGoodsAmountWithoutAnyDeal(String code, int buys) {
        return mapper.updateGoodsAmountWithoutAnyDeal(code,buys)>0?true:false;
    }

}
